﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Utility;
using MyAlbum.DataAccess.LinqToSql;


namespace MyAlbum.Data.Linq
{
    public class EntranceRepository : IRepository<MyAlbum.Model.Entrance>
    {
        private string _connectionString;
        public EntranceRepository(string connectionString)
        {
            Assert.IsNotNull(connectionString);
            this._connectionString = connectionString;
        
        }


        public List<MyAlbum.Model.Entrance> GetItems()
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.Entrances.Select(e =>
                    new MyAlbum.Model.Entrance()
                    {
                        Id = e.Id.ToString(),
                        IP = e.IP,
                        Page = e.Page,
                        SessionId = e.SessionId,
                        Timestamp = e.Timestamp,
                        Details = e.Details
                    }).ToList();
            }
        }

        public void AddItem(MyAlbum.Model.Entrance item)
        {
            Assert.IsNotNull(item);
            Assert.IsNotNull(item.SessionId);
            Assert.IsNotNull(item.IP);
            Assert.IsNotNull(item.Page);
            Entrance e = new Entrance()
            {
                Id=Guid.NewGuid(),
                Timestamp = item.Timestamp,
                SessionId = item.SessionId,
                IP = item.IP,
                Page = item.Page,
                Details = item.Details
            };
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                ImagesDB.Entrances.InsertOnSubmit(e);
                ImagesDB.SubmitChanges();
            }
            item.Id = e.Id.ToString();
        }

        public void DeleteItem(MyAlbum.Model.Entrance item)
        {
            Assert.IsNotNull(item);
            
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                var old = ImagesDB.Entrances.FirstOrDefault(s => s.Id == new Guid(item.Id));
                if (old != null)
                {
                    ImagesDB.Entrances.DeleteOnSubmit(old);
                    ImagesDB.SubmitChanges();
                }
            }
        }

        public void UpdateItem(MyAlbum.Model.Entrance item)
        {
            throw new NotImplementedException();
        }

        public MyAlbum.Model.Entrance GetItemById(string id)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.Entrances.Where(e => e.Id ==new Guid( id)).Select(e =>
                    new MyAlbum.Model.Entrance()
                    {
                        Id = e.Id.ToString(),
                        IP = e.IP,
                        Page = e.Page,
                        SessionId = e.SessionId,
                        Timestamp = e.Timestamp,
                        Details = e.Details
                    }).SingleOrDefault();
            }
        }
    }
}
